#CXX = g++
CXX = mpic++
CPPFLAGS = -I. -I../kernel/include -I../models/uarch/src -I../models -I../models/cache/CaffCache/src -I../models/network/CaffNet/src -I../models/processor/CaffProc/src -I../models/memory/CaffMC/src -I./glue
DBG = -g
LINKER = $(CXX) -ldl -L./qsim/ 

# our macs use g++ 4.2.1, pasta uses g++ 4.1.2
# compilation will die because of a deprecated conversion from string
# constant to char* error
#CXXFLAGS = -O0 $(DBG) -Wall -Werror -Wno-unknown-pragmas -fno-strict-aliasing
CXXFLAGS = $(DBG) -Wall -fno-strict-aliasing -m64 -lpthread #-L ./processor/zesto -lZesto # -lqsim -I../distorm/ -I/usr/local/include

SOURCES:= \
	../models/uarch/src/uarch/node.cpp\
	../models/uarch/src/uarch/module.cpp\
	../models/uarch/src/uarch/settings.cpp\
	./glue/signals.cpp\
	./glue/sim.cpp\
	./glue/sim_stat_manager.cpp\
	../models/uarch/src/uarch/stat_engine.cpp\
	./glue/ticker.cpp\
	../models/processor/preq.cpp
  #./frontend/emulator_interface.cpp\
  #./frontend/thread.cpp

#	./processor/insn.cpp\
#	./processor/simple/preq.cpp\
#	./processor/simple/processor.cpp\
#	./processor/simple/processor_context.cpp\
#	./processor/simple/processor_simple.cpp
#	./processor/preq.cpp\

HEADERS:=$(patsubst %.cpp, %.h, $(SOURCES))
OBJECTS:=$(patsubst %.cpp, %.o, $(SOURCES))
DEPS:=$(patsubst %.cpp, %.d, $(SOURCES))

all: settings $(DEPS) sim_manifold #sim_trace
deps: $(DEPS)

%.d: %.cpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -MM $< > $@ 

include $(wildcard *.d)

settings: ./glue/settings.l ./glue/settings.y
	flex -o./glue/lex.yy.c ./glue/settings.l 
	bison -o./glue/settings.tab.c -d ./glue/settings.y
	$(CXX) $(CPPFLAGS) -o./glue/settings.tab.o -c ./glue/settings.tab.c
	$(CXX) $(CPPFLAGS) -o./glue/lex.yy.o -c ./glue/lex.yy.c


%.o: %.cpp 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o ${OUTOPT} $@

#sim_trace: $(DEPS) $(OBJECTS) ./glue/sim_trace.o ./glue/sim_trace.h	./glue/settings.l ./glue/settings.y
#	${LINKER} $(CXXFLAGS) ./glue/sim_trace.o $(OBJECTS) ./glue/settings.tab.o ./glue/lex.yy.o -o sim_trace -lfl -ly

sim_manifold: $(DEPS) $(OBJECTS) ./glue/sim_manifold.o ./glue/sim_manifold.h ./glue/settings.l ./glue/settings.y 
	#${LINKER} $(CXXFLAGS) ./glue/sim_manifold.o $(OBJECTS) $(MANIFOLD_OBJECTS) processor/zesto/libZesto.a ./glue/settings.tab.o ./glue/lex.yy.o -o sim_manifold -lfl -ly
	${LINKER} $(CXXFLAGS) ./glue/sim_manifold.o $(OBJECTS) $(MANIFOLD_OBJECTS) ./glue/settings.tab.o ./glue/lex.yy.o -o sim_manifold -lfl -ly -L ../models/processor/zesto -lZesto -L../kernel/src -lmanifold -L../models/cache/CaffCache/src/CaffCache -lCaffCache -L../models/network/CaffNet/src/CaffNet -lCaffNet -L../models/memory/CaffMC/src/CaffMC -lCaffMC

#sim_trace: $(DEPS) $(OBJECTS) $(MANIFOLD_OBJECTS) ./glue/sim_trace.o ./glue/sim_trace.h ./glue/settings.l ./glue/settings.y 
	#${LINKER} $(CXXFLAGS) ./glue/sim_trace.o $(OBJECTS) $(MANIFOLD_OBJECTS) ./glue/settings.tab.o ./glue/lex.yy.o -o sim_trace -lfl -ly


## cleaning
clean:
	-rm -rf $(OBJECTS) *~ sim_manifold *.d ./cache/*.o ./frontend/*.o ./glue/*.o ./memory/*.o ./network/*.o ./processor/*.o ./processor/simple/*.o ./glue/lex.yy.c ./glue/settings.tab.* temperature_trace.txt power_trace.txt ./kernel/*.o

